package com.superman.view;

import com.superman.entity.Record_User_Car;
import com.superman.entity.User;
import com.superman.util.CurrentPayment;
import com.superman.util.JPanelRecord;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class ReturnUI {
    private User user;
    Query query = new Query();
    RentAndReturn rentAndReturn = new RentAndReturn();
    JPanelRecord jPanelRecord = new JPanelRecord();

    private JFrame frame = new JFrame("用户还车业务办理");
    private Container container = frame.getContentPane();

    //上侧
    private JPanel upside_panel = new JPanel();
    private JLabel car_lab = new JLabel("请选择您想归还的汽车：");
    private JComboBox<String> car_cb;
    private JButton return_btn = new JButton("确认还车");

    //中部
    private JPanel middle_panel = new JPanel();
    private JPanel return_panel;

    //下侧
    private JPanel downside_panel = new JPanel();
    private JButton backup_btn = new JButton("返回");
    private JButton quit_btn = new JButton("退出");
    private JLabel result_lab = new JLabel("欢迎使用二嗨租车");

    public ReturnUI(User user){
        frame.setSize(1000,500);
        container.setLayout(new BorderLayout());
        initFrame(user);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void initFrame(User user){
        //上侧
        upside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
        List<Record_User_Car> list = query.returnQueryRecord(user.getId());
        Vector<String> myVector = new Vector<>();
        for (Record_User_Car ruc: list){
            String s = ruc.getCar_number() + "-" + ruc.getModel() + "-" + ruc.getColor() + "-" +
                    ruc.getT_comments() + "-" + ruc.getBrand() + "-" + ruc.getCategory() + "-(借车时间)" +
                    ruc.getStart_date() + "-(当前租金总额)" + CurrentPayment.getCurrentPayment(ruc.getStart_date(), ruc.getRent());
            myVector.add(s);
        }
        car_cb = new JComboBox<>(myVector);
        upside_panel.add(car_lab);
        upside_panel.add(car_cb);
        upside_panel.add(return_btn);
        container.add(upside_panel, "North");

        //中
        middle_panel.setLayout(new CardLayout(20, 20));
        container.add(middle_panel, "Center");

        //下侧
        downside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
        downside_panel.add(backup_btn);
        downside_panel.add(quit_btn);
        downside_panel.add(result_lab);
        container.add(downside_panel, "South");

        return_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String choose = String.valueOf(car_cb.getSelectedItem());
                String car_number = choose.split("-")[0];
                String car_id = null;
                for (Record_User_Car ruc:list){
                    if (ruc.getCar_number().equals(car_number)){
                        car_id = ruc.getCar_id();
                    }
                }
                List<Record_User_Car> list = rentAndReturn.returnCar(car_id);
                if (list.isEmpty()){
                    result_lab.setText("还车失败！");
                }else {
                    result_lab.setText("还车成功！");
                    return_panel = jPanelRecord.getJPanelRecord(list);
                    middle_panel.add("还车成功信息", return_panel);
                }
            }
        });

        backup_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new UserUI(user);
            }
        });

        quit_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new LoginUI();
            }
        });
    }

    public void hide(){
        frame.setVisible(false);
    }
}
